/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package client;

import commom.VirtualMachine;
import server.Test;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import commom.Network;
import util.Logger;

/**
 * This class implements a thread that runs all the time, waiting for new connections
 * from a master module
 * @author gravena
 */
public class ConnectionListener implements Runnable {
    private ServerSocket sock;
    private ArrayList<VirtualMachine> vms;
    private ArrayList<TestSlave> test;

    public ConnectionListener(ArrayList<VirtualMachine> vms, ArrayList<TestSlave> test) {
        this.vms = vms;
        this.test = test;
    }

    @Override
    public void run() {
        Socket incoming;

        try {
            /**
             * Keeps waiting for a incoming XML
             */
            this.sock = new ServerSocket(Network.DEFAULT_PORT);
            System.out.println("Server Started");
            while(true) {
                incoming = this.sock.accept();                

                System.out.printf("%s ",Logger.getDate() + " Connection reveived from " + incoming.getInetAddress().getHostAddress());
                new Thread(new DoAction(vms, test, incoming)).start();
            }

        } catch (IOException ex) {
            Logger.add(ex.getMessage());            
        }
    }

}
